package com.demostic.demostic_test.activity;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.ACDeviceActivator;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.base.BaseActivity;
import com.demostic.demostic_test.utils.Constants;
import com.demostic.demostic_test.utils.SettingUtils;
import com.demostic.demostic_test.utils.ToastUtils;
import com.demostic.demostic_test.utils.UserUtils;
import com.demostic.demostic_test.utils.WifiUtils;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class FirstApActivity extends BaseActivity implements View.OnClickListener {
    public static final String EXTRA_PASS = "EXTRA_PASS";
    public static final String EXTRA_SSID = "EXTRA_SSID";
    private final String TAG = FirstApActivity.class.getSimpleName();
    ACDeviceActivator activator;
    private String bssid;
    Button bt_next;
    Context context;
    EditText et_pass;
    private Intent i_ap;
    ImageView image_back;
    ImageView image_show_pass;
    String pass;
    String ssid;
    private String subdomain;
    private int subdomainId;
    private TextView textView;
    TextView tv_set;
    TextView tv_ssid;
    WifiManager wifiManager;

    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(32);
        setContentView(R.layout.activity_ap_first);
        getSubdomain();
        initView();
        initData();
        getPermissions();
    }

    public void getSubdomain() {
        Bundle bundle;
        Intent intent = getIntent();
        if (intent != null && (bundle = intent.getExtras()) != null) {
            this.subdomain = bundle.getString("subdomain");
            this.subdomainId = bundle.getInt("subdomainId");
        }
    }

    public void initData() {
        this.context = this;
        this.activator = AC.deviceActivator(15);
        this.wifiManager = (WifiManager) this.context.getApplicationContext().getSystemService("wifi");
        this.ssid = this.activator.getSSID();
        if (!TextUtils.isEmpty(this.ssid)) {
            this.tv_ssid.setText(this.ssid);
        }
        WifiInfo info = this.wifiManager.getConnectionInfo();
        if (info != null) {
            this.bssid = info.getBSSID();
        }
    }

    private void initView() {
        this.image_back = (ImageView) findViewById(R.id.image_back);
        this.tv_set = (TextView) findViewById(R.id.tv_set);
        this.tv_ssid = (TextView) findViewById(R.id.tv_ssid);
        this.bt_next = (Button) findViewById(R.id.bt_next);
        this.et_pass = (EditText) findViewById(R.id.et_pass);
        this.et_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
        this.image_show_pass = (ImageView) findViewById(R.id.image_show_pass);
        this.textView = (TextView) findViewById(R.id.tv_step4);
        if (this.subdomain.equals(Constants.subdomain_x782) || this.subdomain.equals(Constants.subdomain_x785) || this.subdomain.equals(Constants.subdomain_x786) || this.subdomain.equals(Constants.subdomain_x787)) {
            this.textView.setText(getString(R.string.ap_aty_fourth_step));
        }
        this.image_back.setOnClickListener(this);
        this.tv_set.setOnClickListener(this);
        this.bt_next.setOnClickListener(this);
        this.image_show_pass.setOnClickListener(this);
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back /* 2131755189 */:
                finish();
                return;
            case R.id.tv_set /* 2131755200 */:
                Intent i = new Intent();
                i.setAction("android.net.wifi.PICK_WIFI_NETWORK");
                startActivity(i);
                return;
            case R.id.image_show_pass /* 2131755202 */:
                int currentIndex = this.et_pass.getSelectionStart();
                boolean isSelected = !this.image_show_pass.isSelected();
                this.image_show_pass.setSelected(isSelected);
                if (isSelected) {
                    this.et_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    this.et_pass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                this.et_pass.setSelection(currentIndex);
                return;
            case R.id.bt_next /* 2131755203 */:
                this.ssid = this.tv_ssid.getText().toString();
                this.pass = this.et_pass.getText().toString().trim();
                if (TextUtils.isEmpty(this.ssid)) {
                    ToastUtils.showToast(this.context, getString(R.string.add_aty_no_wifi));
                    return;
                } else if (TextUtils.isEmpty(this.pass)) {
                    ToastUtils.showToast(this.context, getString(R.string.ap_aty_input_pass));
                    return;
                } else if (!UserUtils.rexCheckPassword(this.pass)) {
                    ToastUtils.showToast(this.context, getString(R.string.add_aty_error_char));
                    return;
                } else if (!this.ssid.startsWith("Robot") || !SettingUtils.isTheSameDevice(this.ssid, this.bssid)) {
                    this.i_ap = new Intent(this.context, SecondApActivity.class);
                    this.i_ap.putExtra(EXTRA_SSID, this.ssid);
                    this.i_ap.putExtra(EXTRA_PASS, this.pass);
                    this.i_ap.putExtra("subdomain", this.subdomain);
                    startActivity(this.i_ap);
                    return;
                } else {
                    ToastUtils.showToast(this.context, getString(R.string.select_home_wifi));
                    return;
                }
            default:
                return;
        }
    }

    private void getPermissions() {
        new RxPermissions(this).requestEach("android.permission.ACCESS_COARSE_LOCATION").subscribe(new Consumer<Permission>() {
            /* class com.demostic.demostic_test.activity.FirstApActivity.AnonymousClass1 */

            public void accept(Permission permission) throws Exception {
                if (permission.granted) {
                    WifiInfo info = FirstApActivity.this.wifiManager.getConnectionInfo();
                    if (info != null) {
                        FirstApActivity.this.bssid = info.getBSSID();
                    }
                    FirstApActivity.this.ssid = WifiUtils.getSsid(FirstApActivity.this.context);
                    if (!TextUtils.isEmpty(FirstApActivity.this.ssid)) {
                        FirstApActivity.this.tv_ssid.setText(FirstApActivity.this.ssid);
                    }
                }
            }
        });
    }
}
